# -*- coding: utf-8 -*-

import frappe
import json


@frappe.whitelist()
def get_doc_withmeta(doc_type, doc_name): 
    count = 1
    data = {"doc_type":doc_type,"parameters":[]}    
    doc = frappe.get_doc(doc_type, doc_name)
    skip_field_types = [
        "Section Break",
        "Column Break",
        "Tab Break",
        "update_auto_repeat_reference",
        "Button",
        "Table",
    ]
    for item in doc.meta.fields:
        if item.fieldtype not in skip_field_types:
            item.fieldtype
            new_item = {
                "id": count,
                "name": item.fieldname,
                "type": get_type_map(item.fieldtype),
                "eval": False,
                "pattern": "",
                "expression": "",
                "showOnlyNameType": False,
                "testData": getattr(doc, item.fieldname),
            }
            data["parameters"].append(new_item)
            count += 1
        if item.fieldtype == "Table":
            lines = getattr(doc, item.fieldname)
            lines_json = frappe.as_json(lines)
            # print(lines_json)
            new_item = {
                "id": count,
                "name": item.fieldname,
                "type": "array",
                "eval": False,
                "pattern": "",
                "expression": "",
                "showOnlyNameType": False,
                "testData": lines_json,
                "children": [],
            }
            count += 1
            child_meta = frappe.get_meta(item.options)
            for sub_item in child_meta.fields:
                if sub_item.fieldtype not in skip_field_types:
                    new_sub_item = {
                        "id": count,
                        "name": sub_item.fieldname,
                        "type": get_type_map(sub_item.fieldtype),
                        "eval": False,
                        "pattern": "",
                        "expression": "",
                        "showOnlyNameType": False,
                    }
                    count += 1
                    new_item["children"].append(new_sub_item)
            data["parameters"].append(new_item)
    return data


def get_type_map(data_type):
    if data_type == "Data":
        return "string"
    elif data_type == "Link" or data_type == "Dynamic Link":
        return "string"
    elif data_type == "Check":
        return "boolean"
    elif data_type == "Select":
        return "string"
    elif data_type == "Table":
        return "array"
    elif data_type == "Attach" or data_type == "Attach Image":
        return "string"
    elif data_type == "Text Editor":
        return "rich_text"
    elif data_type == "Date":
        return "date"
    elif data_type == "Date and Time":
        return "string"
    elif data_type == "Barcode":
        return "string"
    elif data_type == "Code":
        return "rich_text"
    elif data_type == "Color":
        return "string"
    elif data_type == "Currency":
        return "number"
    elif data_type == "Float":
        return "number"
    elif data_type == "Geolocation":
        return "string"
    elif data_type == "HTML":
        return "rich_text"
    elif data_type == "Image":
        return "string"
    elif data_type == "Int":
        return "number"
    elif (
        data_type == "Small Text"
        or data_type == "Long Text"
        or data_type == "Text"
        or data_type == "Markdown Editor"
    ):
        return "string"
    elif data_type == "Password":
        return "string"
    elif data_type == "Percent":
        return "number"
    elif data_type == "Rating":
        return "number"
    elif data_type == "Signature":
        return "string"
    elif data_type == "Table MultiSelect":
        return "string"
    elif data_type == "Time":
        return "string"
    elif data_type == "Duration":
        return "string"
    elif data_type == "JSON":
        return "string"
    else:
        return "string"
